﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UI
{
    public class FuncNavItem : MonoBehaviour
    {
        public UILabel titleLabel;
        public UILabel detailLabel;

        FuncNavPage funcNavPage;
        FuncNavKit.FuncNavType itemType;

        int sortIndex;

        public int GetSortIndex()
        {
            return sortIndex;
        }

        public void Init(FuncNavKit.FuncNavType type, FuncNavPage page)
        {
            this.itemType = type;
            this.funcNavPage = page;

            titleLabel.text = GetFuncNavTitle();
            detailLabel.text = GetFuncNavDetail();


            var item = Dict.Blo.DictFuncNavBlo.GetTableNavigationItem(((int)itemType).ToString());
            if(item != null)
            {
                sortIndex = item.Index;
            }
            else
            {
                sortIndex = 0;
            }
        }

        string GetFuncNavTitle()
        {
            var item = Dict.Blo.DictFuncNavBlo.GetTableNavigationItem(((int)itemType).ToString());
            if(item != null)
            {
                return TableTextBlo.GetText(item.Name);
            }
            return "";
        }

        string GetFuncNavDetail()
        {
            var item = Dict.Blo.DictFuncNavBlo.GetTableNavigationItem(((int)itemType).ToString());
            if (item != null)
            {
                return TableTextBlo.GetText(item.Desc);
            }
            return "";
        }

        string GetFuncNavNpcId()
        {
            var item = Dict.Blo.DictFuncNavBlo.GetTableNavigationItem(((int)itemType).ToString());
            if (item != null)
            {
                return item.NpcId;
            }
            return "";
        }

        public void OnGotoButtonClick()
        {
            if (!string.IsNullOrEmpty(GetFuncNavNpcId()))
            {
                var npc = Dict.Blo.DictFuncNavBlo.GetTableNpc(GetFuncNavNpcId());
                var transPos =  new Vector3((float)npc.transferX, (float)npc.transferY, (float)npc.transferZ);
                StartCoroutine(Goto(transPos));
            }
            else
            {
                switch (itemType) {
                    case FuncNavKit.FuncNavType.HAVE_FORMATION_SOLT:
                    case FuncNavKit.FuncNavType.HAVE_HELP_FORMATION_SOLT:
                        PageManager.Instance.OpenPage("ArrayPage", "");
                        break;
                    case FuncNavKit.FuncNavType.STAMINA_LEFT:
                        PageManager.Instance.OpenPage("UICopy", "");
                        break;
                    case FuncNavKit.FuncNavType.TIREDNESS_LEFT:
                        var area = Dict.Blo.DictFuncNavBlo.GetTableArea("area_25");
                        var transPos = new Vector3((float)area.transferX, (float)area.transferY, (float)area.transferZ);
                        StartCoroutine(GotoDoor(transPos));
                        break;
                }
            }

        }

        IEnumerator GotoDoor(Vector3 transferPos)
        {
            yield return StartCoroutine(_Goto(transferPos));
            var self = Room.RoomManager.currentRoom.charactorManager.GetSelfCharactor();
            var selfAI = self.ai as Room.RCAISelf;
            selfAI.AutoGoto(new Vector3(43.6f, -2.2f, -45.4f), null);
            funcNavPage.Close();

        }

        IEnumerator Goto(Vector3 transferPos)
        {
            yield return StartCoroutine(_Goto(transferPos));
            var npc = Room.RoomManager.currentRoom.charactorManager.GetCharactor(GetFuncNavNpcId());
            if (npc != null)
            {
                Room.RoomManager.currentRoom.uiManager.ClickCharactor(npc);
            }
            funcNavPage.Close();
        }

        IEnumerator _Goto(Vector3 transferPos)
        {
            TouchKit.SetEnabled(false);
            var TopMask = Instantiate(Resources.Load("prefab/TopMask")) as GameObject;
            TopMask.transform.SetParent(GameObject.Find("/UI Root").transform);
            TopMask.SetActive(true);


            GameObject whitePanel = GameObject.Instantiate(Resources.Load("prefab/WhitePanel")) as GameObject;
            whitePanel.transform.SetParent(GameObject.Find("/UI Root").transform);
            whitePanel.transform.localPosition = Vector3.zero;
            whitePanel.transform.localScale = Vector3.one;
            funcNavPage.transform.localPosition = new Vector3(10000, 10000, 10000);

            // 播放传送阵开启特效
            string transParticlePath = "particles/" + Dict.Blo.DictParticleBlo.GetCommonParticle("Chuansongzhen1").ResourcePath;
            yield return StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadAssetBundleCoroutine(transParticlePath));
            GameObject asset0 = AssetLoad.AssetBundleManager.Instance.GetAssetFromBundle(transParticlePath, CommonMethod.GetAssetNameFromAssetBundleName(transParticlePath), typeof(GameObject)) as GameObject;
            var particleObj0 = GameObject.Instantiate(asset0) as GameObject;

            GameObject transObj = new GameObject();
            particleObj0.transform.SetParent(transObj.transform);
            particleObj0.transform.localPosition = Vector3.zero;
            particleObj0.transform.localEulerAngles = Vector3.zero;
            particleObj0.transform.localScale = Vector3.one;

            var self = Room.RoomManager.currentRoom.charactorManager.GetSelfCharactor();
            transObj.transform.position = self.transform.position;
            self.gameObject.SetActive(false);
            if ((self.ui as Room.RCUIPlayer).Name.title.isActiveAndEnabled)
            {
                (self.ui as Room.RCUIPlayer).Name.title.gameObject.SetActive(false);
            }

            AudioManager.Instance.PlayUIAudioChannel("transferAppear");

            yield return new WaitForSeconds(1.3f);
            // 播放白屏
            var whiteMask = whitePanel.transform.Find("WhiteMask");
            whiteMask.GetComponent<UITweener>().ResetToBeginning();
            whiteMask.GetComponent<UITweener>().PlayForward();

            yield return new WaitForSeconds(1f);
            // 删除传送特效
            GameObject.Destroy(transObj);
            // 移动主角坐标
            var transPos = transferPos;
            self.transform.position = transPos;

            // 通信服务器
            var town = Room.RoomManager.currentRoom as Room.RoomTown;
            ManagerController.Instance.TrySceneTransfer(ServerScene.Town, transPos, null, () => {
                town.netManager.SendCSCurrentPosition(null);
            });

            var transEndParticleDict = Dict.Blo.DictParticleBlo.GetCommonParticle("Chuansongzhen2");
            string transEndParticlePath = "particles/" + transEndParticleDict.ResourcePath;
            yield return StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadAssetBundleCoroutine(transEndParticlePath));
            var asset1 = AssetLoad.AssetBundleManager.Instance.GetAssetFromBundle(transEndParticlePath, CommonMethod.GetAssetNameFromAssetBundleName(transEndParticlePath), typeof(GameObject)) as GameObject;
            GameObject particleObj1 = GameObject.Instantiate(asset1) as GameObject;
            GameObject transEndObj = new GameObject();
            particleObj1.transform.SetParent(transEndObj.transform);
            particleObj1.transform.localPosition = Vector3.zero;
            particleObj1.transform.localScale = Vector3.one;
            particleObj1.transform.localEulerAngles = Vector3.zero;

            transEndObj.transform.position = transPos;

            AudioManager.Instance.PlayUIAudioChannel("transferDisappear");

            var texture = whiteMask.GetComponent<UITexture>();
            yield return new WaitForSeconds(0.1f);

            texture.alpha = 0f;

            self = Room.RoomManager.currentRoom.charactorManager.GetSelfCharactor();
            self.gameObject.SetActive(true);
            if ((self.ui as Room.RCUIPlayer).Name.title.isActiveAndEnabled)
            {
                (self.ui as Room.RCUIPlayer).Name.title.gameObject.SetActive(true);
            }
         
            Destroy(whitePanel);
            yield return new WaitForSeconds(1.8f);
            Destroy(TopMask);
            TouchKit.SetEnabled(true);
        }


    }
}
